package com.sdsy.controller;


/**
 * (TransferAccountsRecord)表控制层
 *
 * @author yxc
 * @since 2018-09-03 11:52:02
 */

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.sdsy.constant.WalletConfig;
import com.sdsy.domain.TransferAccountsExcelDO;
import com.sdsy.service.TransferAccountsExcelService;
import com.sdsy.util.*;
import com.sdsy.vo.TransferAccountsExcelVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/transferAccountsExcel")
//@PropertySource("classpath:config/config.properties")
public class TransferAccountsExcelController {
    private static Logger logger = LoggerFactory.getLogger(TransferAccountsExcelController.class);

    @Autowired
    private WalletConfig walletConfig;
    @Autowired
    private TransferAccountsExcelService transferAccountsExcelService;

    @GetMapping("/excelToPage")
    String FcpfMerManage(Model model) {
        return "/uploadExcel";
    }

    /**
     * 查询用户代币余额
     *
     * @param
     */
    @ResponseBody
    @RequestMapping(value = "/excelForData", method = {RequestMethod.GET, RequestMethod.POST})
    public R excelForData(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        String msg = "文件导入成功！";
        InputStream in = null;
        List<TransferAccountsExcelDO> listob = null;
        MultipartFile file = multipartRequest.getFile("upfile");
        if (file.isEmpty()) {
            return null;
        }
        try {
            in = file.getInputStream();
            listob = transferAccountsExcelService.excelForData(in, file.getOriginalFilename());

        } catch (IOException e) {
            e.printStackTrace();
        }


        return R.ok();
    }


    /**
     * 查询导入的数据列表
     */
    @PostMapping("/queryList")
    @ResponseBody
    public String queryList() {
        List<TransferAccountsExcelDO> list = transferAccountsExcelService.queryList();
        if (SysUtil.isNull(list) || list.size() == 0) {
            list = new ArrayList<TransferAccountsExcelDO>();
        }
        return JSONUtil.toJsonStr(list);
    }


    @ResponseBody
    @GetMapping("/list")
    public PageResult list(@RequestParam Map<String, Object> params) {

        //查询列表数据
        PageResult pageResult = null;
        pageResult = PageFormatHelper.pageHelper(transferAccountsExcelService, params);
        return pageResult;
    }

    /**
     * 更新hash值
     */
    @PostMapping("/updateTransferAccounts")
    @ResponseBody
    public String updateTransferAccounts(TransferAccountsExcelDO vo) {
        String remark = "";
        boolean flag = transferAccountsExcelService.updateTransferAccounts(vo);
        if (flag == true) {
            remark = "1";
        } else {
            remark = "2";
        }
        return remark;
    }


    /**
     *
     */
    @PostMapping("/deleteAccount")
    @ResponseBody
    public boolean deleteAccount(TransferAccountsExcelDO vo) {

        Long id = vo.getId();
        try {
            return transferAccountsExcelService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();

        }
           return false;
    }

}